import axios from 'axios'
import { ElMessage } from 'element-plus'
import _ from 'lodash'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import ResultCode from './resultcode'
import cfg from './config'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'

const service = axios.create({
    baseURL: import.meta.env.VITE_BASE_URL + 'api',
    timeout: 30000,
})

const showLoading = () => {
    NProgress.start()
}

const closeLoading = () => {
    NProgress.done()
}

service.interceptors.request.use(
    config => {
        showLoading()
        const store = useUserStore()
        config.headers.set({
            'appkey': cfg.appkey,
            'Authorization': cfg.tokenHead + store.userToken,
        })
        return config
    },
    error => {
        closeLoading()
        ElMessage({
            showClose: true,
            message: error,
            type: 'error',
        })
        return Promise.reject(error)
    }
)

service.interceptors.response.use(
    response => {
        closeLoading()
        const result = response.data
        if (result.code == ResultCode.SUCCESS) {
            return result
        } else {
            let msg = result.message;
            if (result.errors) {
                msg += ':'
                _.values(result.errors).forEach(items => {
                    msg += items.join(',')
                })
            }
            ElMessage({
                showClose: true,
                message: msg,
                duration: 5000,
                type: 'error',
                onClose: () => {
                    if (result.code == ResultCode.AUTH_ERROR) {
                        const store = useUserStore()
                        const router = useRouter()
                        store.logout()
                        //返回登录
                        router.push({
                          name: 'login'  
                        })
                    }
                }
            })
            return Promise.reject(result)
        }
    },
    error => {
        closeLoading()
        console.log(error)
        if (error.response && error.response.code == ResultCode.AUTH_ERROR) {
            ElMessage({
                showClose: true,
                message: error.response.data.message,
                type: 'error',
                onClose: () => {
                    const store = useUserStore();
                    const router = useRouter()
                    store.logout()
                    //返回登录
                    router.push({
                        name: 'login'
                    })
                }
            })
        } else if (error.response && error.response.data) {
            ElMessage({
                showClose: true,
                message: error.response.data.message,
                type: 'error'
            })
        } else {
            ElMessage({
                showClose: true,
                message: error,
                type: 'error',
            })
        }
        return Promise.reject(error)
    }
)

export default service